package code

import (
	"fmt"
)

func Array1() {
	// 默认数组中的值是类型默认值
	var arr [3]int
	// 通过下标取值
	fmt.Printf("arr[0]: %d\n", arr[0])
	fmt.Printf("arr[1]: %d\n", arr[1])
	fmt.Printf("arr[2]: %d\n", arr[2])
	fmt.Printf("=====================\n")

	arr = [3]int{1, 2}
	fmt.Printf("arr[0]: %d\n", arr[0])
	fmt.Printf("arr[1]: %d\n", arr[1])
	fmt.Printf("arr[2]: %d\n", arr[2])
	fmt.Printf("=====================\n")

	// ... 自动失败数组大小
	arr1 := [...]int{1, 2, 3, 4}
	for index, value := range arr1 {
		fmt.Printf("=====index: %d\n", index)
		fmt.Printf("value: %d\n", value)
	}

	fmt.Printf("=====================\n")
	arr1 = [...]int{2: 3, 4}
	for index, value := range arr1 {
		fmt.Printf("=====index: %d\n", index)
		fmt.Printf("value: %d\n", value)
	}
}

func Array2() {
	var arr [4]int
	changeArray(arr)
	fmt.Printf("修改后 arr: %#v\n", arr)

	changeArrayPoint(&arr)
	fmt.Printf("指针修改后 arr: %#v\n", arr)

	slice := arr[0:4]
	changeSliceArray(slice)
	fmt.Printf("切片修改后 arr: %#v\n", arr)
}

func changeArray(arr [4]int) {
	arr[0] = 10
}

func changeArrayPoint(arr *[4]int) {
	arr[0] = 10
}

func changeSliceArray(arr []int) {
	arr[0] = 10
}
